<template>
  <el-dialog :title="textMap[dialogStatus]" :visible="dialogFormVisible" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="80px" style="margin-left:50px;margin-right:50px;">
      <el-form-item label="ID" prop="id">
        <span v-if="isView">{{ form.id }}</span>
        <el-input v-else v-model="form.id" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <span v-if="isView">{{ form.title }}</span>
        <el-input v-else v-model="form.title" :disabled="isView"/>
      </el-form-item>
      <el-form-item label="点击量" prop="pageviews" >
        <span v-if="isView">{{ form.pageviews }}</span>
        <el-input v-else v-model.number="form.pageviews"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <span v-if="isView">{{ form.status }}</span>
        <el-input v-else v-model="form.status"/>
      </el-form-item>
      <el-form-item label="发布时间" prop="display_time">
        <span v-if="isView">{{ form.display_time | parseTime('{y}-{m}-{d}') }}</span>
        <el-date-picker
          v-else
          :editable="false"
          v-model="form.display_time"
          :disabled="isView"
          type="date"
          placeholder="发布时间"
        />
      </el-form-item>
      <el-form-item v-if="!isView" label="下拉框" prop="select">
        <el-select v-model="form.select" :disabled="isView" placeholder="请选择XX">
          <el-option label="上海" value="shanghai"/>
          <el-option label="北京" value="beijing"/>
          <el-option label="广州" value="guangzhou"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="!isView" label="下拉树">
        <el-cascader
          :options="options"
          v-model="selectedOptions"
          expand-trigger="hover"
          filterable
        />
      </el-form-item>
      <el-form-item v-if="!isView" label="开关" prop="switch">
        <el-switch v-model="form.switch" :disabled="isView"/>
      </el-form-item>
      <el-form-item v-if="!isView" label="复选框" prop="type">
        <el-checkbox-group v-model="form.type" :disabled="isView">
          <el-checkbox label="选项一" name="type"/>
          <el-checkbox label="选项二" name="type"/>
          <el-checkbox label="选项三" name="type"/>
          <el-checkbox label="选项四" name="type"/>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item v-if="!isView" label="单选框" prop="resource">
        <el-radio-group v-model="form.resource" :disabled="isView">
          <el-radio label="选项一"/>
          <el-radio label="选项二"/>
          <el-radio label="选项三"/>
          <el-radio label="选项四"/>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="!isView" label="文本域" prop="desc">
        <el-input v-model="form.desc" :disabled="isView" type="textarea"/>
      </el-form-item>
    </el-form>
    <div v-if="!isView" slot="footer" cslass="dialog-footer">
      <el-button v-waves @click="cancel">取消</el-button>
      <el-button v-waves @click="resetForm">重置</el-button>
      <el-button v-waves type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { parseTime } from '@/utils'
export default {
  name: 'TemplateListEdit',
  filters: {
    parseTime(val) {
      return val ? parseTime(val, '{y}-{m}-{d}  {h}:{i}:{s}') : ''// '{y}-{m}-{d}  {h}:{i}:{s}'为默认格式，可不填
    }
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
      required: true
    },
    form: {
      type: Object,
      default: () => {
        return {
          id: '',
          title: '',
          pageviews: '',
          status: '',
          display_time: null,
          select: '',
          switch: null,
          type: [],
          resource: [],
          desc: ''
        }
      }
    },
    isView: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectedOptions: [],
      textMap: {
        view: '查看',
        update: '编辑',
        create: '新增'
      },
      options: [
        {
          value: 'zhinan',
          label: '指南',
          children: [
            {
              value: 'shejiyuanze',
              label: '设计原则',
              children: [
                {
                  value: 'yizhi',
                  label: '一致'
                },
                {
                  value: 'fankui',
                  label: '反馈'
                },
                {
                  value: 'xiaolv',
                  label: '效率'
                },
                {
                  value: 'kekong',
                  label: '可控'
                }
              ]
            },
            {
              value: 'daohang',
              label: '导航',
              children: [
                {
                  value: 'cexiangdaohang',
                  label: '侧向导航'
                },
                {
                  value: 'dingbudaohang',
                  label: '顶部导航'
                }
              ]
            }
          ]
        },
        {
          value: 'zujian',
          label: '组件',
          children: [
            {
              value: 'basic',
              label: 'Basic',
              children: [
                {
                  value: 'layout',
                  label: 'Layout 布局'
                },
                {
                  value: 'color',
                  label: 'Color 色彩'
                },
                {
                  value: 'typography',
                  label: 'Typography 字体'
                },
                {
                  value: 'icon',
                  label: 'Icon 图标'
                },
                {
                  value: 'button',
                  label: 'Button 按钮'
                }
              ]
            },
            {
              value: 'form',
              label: 'Form',
              children: [
                {
                  value: 'radio',
                  label: 'Radio 单选框'
                },
                {
                  value: 'checkbox',
                  label: 'Checkbox 多选框'
                },
                {
                  value: 'input',
                  label: 'Input 输入框'
                },
                {
                  value: 'input-number',
                  label: 'InputNumber 计数器'
                },
                {
                  value: 'select',
                  label: 'Select 选择器'
                },
                {
                  value: 'cascader',
                  label: 'Cascader 级联选择器'
                },
                {
                  value: 'switch',
                  label: 'Switch 开关'
                },
                {
                  value: 'slider',
                  label: 'Slider 滑块'
                },
                {
                  value: 'time-picker',
                  label: 'TimePicker 时间选择器'
                },
                {
                  value: 'date-picker',
                  label: 'DatePicker 日期选择器'
                },
                {
                  value: 'datetime-picker',
                  label: 'DateTimePicker 日期时间选择器'
                },
                {
                  value: 'upload',
                  label: 'Upload 上传'
                },
                {
                  value: 'rate',
                  label: 'Rate 评分'
                },
                {
                  value: 'form',
                  label: 'Form 表单'
                }
              ]
            },
            {
              value: 'data',
              label: 'Data',
              children: [
                {
                  value: 'table',
                  label: 'Table 表格'
                },
                {
                  value: 'tag',
                  label: 'Tag 标签'
                },
                {
                  value: 'progress',
                  label: 'Progress 进度条'
                },
                {
                  value: 'tree',
                  label: 'Tree 树形控件'
                },
                {
                  value: 'pagination',
                  label: 'Pagination 分页'
                },
                {
                  value: 'badge',
                  label: 'Badge 标记'
                }
              ]
            },
            {
              value: 'notice',
              label: 'Notice',
              children: [
                {
                  value: 'alert',
                  label: 'Alert 警告'
                },
                {
                  value: 'loading',
                  label: 'Loading 加载'
                },
                {
                  value: 'message',
                  label: 'Message 消息提示'
                },
                {
                  value: 'message-box',
                  label: 'MessageBox 弹框'
                },
                {
                  value: 'notification',
                  label: 'Notification 通知'
                }
              ]
            },
            {
              value: 'navigation',
              label: 'Navigation',
              children: [
                {
                  value: 'menu',
                  label: 'NavMenu 导航菜单'
                },
                {
                  value: 'tabs',
                  label: 'Tabs 标签页'
                },
                {
                  value: 'breadcrumb',
                  label: 'Breadcrumb 面包屑'
                },
                {
                  value: 'dropdown',
                  label: 'Dropdown 下拉菜单'
                },
                {
                  value: 'steps',
                  label: 'Steps 步骤条'
                }
              ]
            },
            {
              value: 'others',
              label: 'Others',
              children: [
                {
                  value: 'dialog',
                  label: 'Dialog 对话框'
                },
                {
                  value: 'tooltip',
                  label: 'Tooltip 文字提示'
                },
                {
                  value: 'popover',
                  label: 'Popover 弹出框'
                },
                {
                  value: 'card',
                  label: 'Card 卡片'
                },
                {
                  value: 'carousel',
                  label: 'Carousel 走马灯'
                },
                {
                  value: 'collapse',
                  label: 'Collapse 折叠面板'
                }
              ]
            }
          ]
        },
        {
          value: 'ziyuan',
          label: '资源',
          children: [
            {
              value: 'axure',
              label: 'Axure Components'
            },
            {
              value: 'sketch',
              label: 'Sketch Templates'
            },
            {
              value: 'jiaohu',
              label: '组件交互文档'
            }
          ]
        }
      ]
    }
  },
  computed: {
    rules() {
      if (this.isView) {
        return {}
      } else {
        return {
          id: [
            { required: true, message: '请输入ID', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' },
            { pattern: /^\d+$/, message: 'ID格式有误，必须为数字', trigger: 'change' }
          ],
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          pageviews: [
            { required: true, message: '请输入点击量', trigger: 'blur' },
            { type: 'number', message: '点击量必须为数字值' }
          ],
          status: [
            { required: true, message: '请输入状态', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          display_time: [
            {
              type: 'date',
              required: true,
              message: '请选择日期',
              trigger: 'change'
            }
          ],
          select: [
            { required: true, message: '请选择下拉框', trigger: 'change' }
          ],
          type: [
            {
              type: 'array',
              required: true,
              message: '请至少选择一个复选框',
              trigger: 'change'
            }
          ],
          resource: [
            { required: true, message: '请选择单选框', trigger: 'change' }
          ],
          desc: [{ required: true, message: '请填写文本域', trigger: 'blur' }]
        }
      }
    }
  },
  methods: {
    resetForm() {
      this.$emit('reset')
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 触发主页面reload
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel() {
      this.$emit('cancel')
    },
    clearValidate() {
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    close() {
      this.$emit('cancel')
    }
  }
}
</script>
